package edu.whu.rose.clock.ssql.binding;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;

import edu.whu.rose.clock.ssql.graphindex.GraphIndexItem;
import edu.whu.rose.clock.ssql.util.SequentialIntArray;

public class GraphIndexItemBinding extends TupleBinding {

	public Object entryToObject(TupleInput input) {
		int size = input.readInt();
		int[] graphs = new int[size];
		SequentialIntArray[] vertexGroups = new SequentialIntArray[size];
		double[] tfs = new double[size];
		for (int i = 0; i < size; i++) {
			graphs[i] = input.readInt();
			int groupSize = input.readInt();
			int[] arr = new int[groupSize];
			for (int j = 0; j < groupSize; j++) {
				arr[j] = input.readInt();
			}
			vertexGroups[i] = new SequentialIntArray(groupSize, arr);
//			tfs[i] = input.readDouble();
		}
		long capacity = input.readLong();
		return new GraphIndexItem(size, graphs, vertexGroups, capacity, tfs);
	}
	
	public void objectToEntry(Object object, TupleOutput output) {
		GraphIndexItem gii = (GraphIndexItem)object;
		output.writeInt(gii.getSize());
		for (int i = 0; i < gii.getSize(); i++) {
			output.writeInt(gii.getGraph(i));
			output.writeInt(gii.getVertexGroupSize(i));
			for (int j = 0; j < gii.getVertexGroupSize(i); j++) {
				output.writeInt(gii.getVertex(i, j));
			}
//			output.writeDouble(gii.getTf(i));
		}
		output.writeLong(gii.getCapacity());
	}

}
